﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading;
using System.Windows.Forms;
using MiniCMS.Configuration;
using CarolLib.Control;
using CarolLib;

namespace MiniCMS
{
    public partial class frmMain : Form
    {
        Dictionary<Guid, MdiUserControl> Mdis;
        MenuStrip menuMain;


        public frmMain()
        {
            InitializeComponent();
        }

        private void frmMain_Load(object sender, EventArgs e)
        {
            this.Mdis = ModuleHelper.LoadALLModules();
            CheckPower(ref this.Mdis);

            menuMain = ModuleHelper.CreateModuleMenu(Mdis.Values.ToList(), subMenu_Click);
            menuMain.Font = this.Font;
            this.Controls.Add(menuMain);

            MdiUserControl defaultModule = ModulesConfiguration.GetConfig().DefaultModule;

            if (defaultModule != null && Mdis.Values.Where(f => f.ModuleID == defaultModule.ModuleID).Count() > 0)
            {
                this.mdiTabCantainer1.AddMdiForm(defaultModule);
            }

            this.tssWelcome.Text = string.Format(this.tssWelcome.Text, AuthUserContext.Instance.User.FullName);

            if (DateTime.Now.DayOfWeek.ToString() == "Monday")
            {
                MessageBox.Show("请您定期备份数据库，以免发生数据丢失！","备份提醒", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        void subMenu_Click(object sender, EventArgs e)
        {
            Guid id = (Guid)(sender as ToolStripMenuItem).Tag;
            this.mdiTabCantainer1.AddMdiForm(Mdis[id]);
        }

        private void frmMain_FormClosed(object sender, FormClosedEventArgs e)
        {
        }

        private void frmMain_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (e.CloseReason == CloseReason.UserClosing)
            {
                if (DialogResult.OK == MessageBox.Show("您确定要退出系统吗?", "提示", MessageBoxButtons.OKCancel))
                {
                    Application.Exit();
                }
                else
                {
                    e.Cancel = true;
                }
            }
        }

        private void tsdLogout_Click(object sender, EventArgs e)
        {
            if (DialogResult.OK == MessageBox.Show("您确定要退出系统吗?", "提示", MessageBoxButtons.OKCancel))
            {
                Application.Restart();
            }
        }

        void CheckPower(ref Dictionary<Guid, MdiUserControl> Mdis)
        {
            Dictionary<Guid, MdiUserControl> validModule = new Dictionary<Guid, MdiUserControl>();

            foreach (var mdi in Mdis)
            {
                if (mdi.Value.PowerFlag == 0 || (AuthUserContext.Instance.User.PowerFlag & mdi.Value.PowerFlag) != 0)
                {
                    validModule.Add(mdi.Key, mdi.Value);
                }
            }

            Mdis = validModule;
        }
    }
}
